package cn.flant.db.hdle;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import cn.flant.db.hdle.dialect.OracleDialect;

public class ResultDataHandler implements ResultSetHandler<ResultData> {

	@Override
	public ResultData handle(ResultSet rs) throws SQLException {
		if (rs == null) {
			return null;
		}
		ResultSetMetaData meta = rs.getMetaData();
		ResultData datas = new ResultData(rs.getFetchSize());
		int column = meta.getColumnCount();
		if (OracleDialect.PAGED_ROWNUM.equalsIgnoreCase(meta.getColumnName(column))) {
			column--;
		}
		String[] title = new String[column];
		for (int i = 1; i <= column; i++) {
			title[i - 1] = meta.getColumnLabel(i);
		}
		datas.setTitle(title);
		while (rs.next()) {
			Object[] values = new Object[column];
			for (int i = 1; i <= column; i++) {
				values[i - 1] = rs.getObject(i);
			}
			datas.addData(values);
		}
		return datas;
	}
}
